# coding: utf-8

import csv
import pylab as pl
import numpy as np


class ApData(object):
    def __init__(self, file_path):
        self.file_path = file_path
        self.ap_dict = {}
        self.data = csv.reader(file(file_path, 'rb'))

    def load_data(self):
        i = 0
        for item in self.data:
            # 去掉表头
            if i == 0:
                i += 1
                continue
            connect_num = item[1]
            stime = item[2]
            ap_mac = item[3]

            hour_min_str = stime[11:16]
            min_time = int(hour_min_str[0:2])*60 + int(hour_min_str[3:5])
            month_day_str = stime[5:10]

            if ap_mac not in self.ap_dict.keys():
                day_dict = {}
                hour_min_dict = {}
                hour_min_dict[min_time] = connect_num
                day_dict[month_day_str] = hour_min_dict
                self.ap_dict[ap_mac] = day_dict
            else:
                temp_ap = self.ap_dict[ap_mac]
                if month_day_str not in temp_ap:
                    hour_min_dict = {}
                    hour_min_dict[min_time] = connect_num
                    temp_ap.setdefault(month_day_str, hour_min_dict)
                else:
                    self.ap_dict[ap_mac][month_day_str].setdefault(min_time, connect_num)
            i += 1
            print 'ok', ':', i

    def draw_graph(self, ap_mac, day_list, time_interval):
        '''
        :param ap_mac: 需要显示的AP的mac地址。
        :param day_list: 显示的天，例如[10,11,12]，是要显示10号，11号，12号
        :param time_interval: 时间区间，例如['03-00','07-00']，是要显示3:00到7:00 的点
        :return:
        '''
        figure_num = 1
        temp_ap = self.ap_dict[ap_mac]
        upper_bound = int(time_interval[1][0:2])*60 + int(time_interval[1][3:5])
        lower_bound = int(time_interval[0][0:2])*60 + int(time_interval[0][3:5])

        for i in temp_ap.keys():
            x_time = []
            y_ap_num = []
            day_num = int(i[3:5])

            if day_num in day_list:
                ap_sorted_keys = sorted(temp_ap[i].keys())
                # for time in temp_ap[i].keys():
                for time in ap_sorted_keys:
                    if (time <= upper_bound) and (time >= lower_bound):
                        x_time.append(time)
                        y_ap_num.append(temp_ap[i][time])
                    print time, ':', temp_ap[i][time]
                # 设置表
                # 下面那句决定是否在同一张图里面显示多条折线。
                # pl.figure(figure_num)
                pl.title(i)
                pl.plot(x_time, y_ap_num)
                figure_num += 1
        pl.show()

if __name__ == "__main__":
    # main program
    CSV_filepath = r'..\..\..\data\data\WIFI_AP_Passenger_Records_chusai_1stround.csv'
    Data = ApData(CSV_filepath)

    ap_name = '5869.6c57.e0b3'
    draw_day_list = [10, 11, 12, 13]
    draw_time_interval = ["18-00", "22-00"]

    Data.load_data()
    Data.draw_graph(ap_name, draw_day_list, draw_time_interval)
    # print Data.ap_dict
    print 'hello'
